package me.trytofeel.controller;

import me.trytofeel.bean.User;
import me.trytofeel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping(produces = "application/json;charset=utf-8")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("selectAllUsers")
    public List<User> selectAllUsers() {
        return userService.selectAllUsers();
    }

    // 根据编号查询用户，网页里要修改用户，要先查这个用户，再回显到表单
    @RequestMapping("selectUserById")
    // 查询和删除，都是用url里通过?来通过get方式拼接参数
    public User selectUserById(int id) {
        return userService.selectUserById(id);
    }

    // 根据编号删除用户
    @RequestMapping(value = "deleteUserById")
    public String deleteUserById(int id) {
        return userService.deleteUserById(id) > 0 ? "删除成功" : "删除失败";
    }

    // 添加新用户，要让浏览器发送一个json对象，加@RequestBody
    @RequestMapping("insertUser")
    public String insertUser(@RequestBody User user) {
        return userService.insertUser(user) > 0 ? "添加成功" : "添加失败";
    }

    // 修改旧用户
    @RequestMapping("updateUser")
    public String updateUser(@RequestBody User user) {
        return userService.updateUser(user) > 0 ? "修改成功" : "修改失败";
    }

    // 根据用户名查询用户，用来模糊查询，返回集合
    @RequestMapping("selectUserByName")
    public List<User> selectUserByName(String username) {
        return userService.selectUserByName(username);
    }


}
